$(function() {

	
	var questionCount = 10.0; //抽得题目数
	var everyScore = 100.0 / questionCount; //每题分数

	var currentQuestion = []
	var count = 0
	var countTime = 10
	var isConfirm = false
	var score = 0

	var allQuestion = [{
			"answer": 2,
			"options": [
				"丹顶鹤 ",
				"大熊猫",
				" 藏羚羊",
				"扬子鳄"
			],
			"quiz": "世界动物基金会的会标是（）"
		},

		{
			"answer": 1,
			"options": [
				" 5月22日 ",
                                                                " 6月22日 ",
                                                                " 7月22日 ",
				" 8月22日 "
			],
			"quiz": "世界野生动物保护日是（）"
		},

		{
			"answer": 3,
			"options": [
				"一级",
				"二级",
				"三级",
				"四级"
			],
			"quiz": "我国的国家级重点保护动物共分（  ）级"
		},

		{
			"answer": 3,
			"options": [
				"80万",
				"100万",
				"120万"
			],
			"quiz": "一只燕子仅一个夏季就能捕捉（）只苍蝇，蚊子，对控制疾病有重要作用 "
		},

		{
			"answer": 2,
			"options": [
				"11.2%",
				"13.1%",
				"16.8%"
			],
			"quiz": "我国现有鸟类1371种，占世界鸟类总数（）"
		},

		{
			"answer":1,
			"options": [
				"生物圈",
				"海洋生态系统",
				"草原生态系统",
				"陆地生态系统"
			],
			"quiz": "地球上最大的生态系统是（）"
		},

		{
			"answer": 1,
			"options": [
				"1972",
				"1973",
				"1974",
				"1975"
			],
			"quiz": "联合国于（）年发表了《人类环境宣言》"
		},

		{
			"answer": 2,
			"options": [
				"对",
				"错"
			],
			"quiz": "“家养”的动物都不是野生动物？"
		},
	
                                {
			"answer": 2,
			"options": [
                                                                "市",
				"县",
				"省"
			],
			"quiz": "猎捕非国家重点保护野生动物的，应当依法取得（）级以上地方人民政府野生动物保护主管部门核发的狩猎证，并且服从猎捕量限额管理。"
		},

                                {
			"answer":4 ,
			"options": [
                                                                "见到违法者立即向野生动物协会反映  ",
				"设立自然保护区",
				"不要穿珍惜动物毛皮做的服装",
                                                                "以上都是"
			],
			"quiz": "怎样保护野生动物？"
		},

                                {
			"answer":1 ,
			"options": [
                                                                "蜂猴",
				"斑羚",
                                                                "河麂 ",
				"以上都是"
			],
			"quiz": "国家一级保护动物有哪些？"
		},

                                {
			"answer": 1,
			"options": [
                                                                "3159-4715",
				"4159-5715",
				"5715-6159",
				"6159-7169"
			],
			"quiz": "野生的孟加拉虎的数量大约（）头？"
		},

                                {
			"answer": 3,
			"options": [
                                                                "东北虎",
				"印度虎",
				"华南虎",
				"巴里虎"
			],
			"quiz": "（）是所有种类的老虎中最为濒临灭绝的一种"
		},

                                {
			"answer":4 ,
			"options": [
                                                                "俄罗斯",
				"缅甸",
				"中国",
				"印度"
			],
			"quiz": "孟加拉虎主要生活在（）"
		},

                                {
			"answer": 3,
			"options": [
                                                                "环境效应",
				"文化价值",
				"观赏价值"
			],
			"quiz": "保护野生动植物有很多意义，不属于其意义的是（）"
		},

                                {
			"answer": 1,
			"options": [
                 "一级重点保护动物和二级重点保护野生动物",
				"重点保护动物和次重点保护动物",
				"濒危野生动物和珍贵野生动物"
			],
			"quiz": "国家对珍贵、濒危的野生动物实行重点保护，国家重点保护的野生动物分为（）"
		},

                                {
			"answer": 1,
			"options": [
                "1980年12月25日 ",
				"1980年5月22日",
				"1980年9月26日 "
			],
			"quiz": "我国于（）加入《濒危野生动植物种国际贸易公约》。"
		},

                                {
			"answer":2 ,
			"options": [
                "新疆北鲵",
				"新疆虎",
				"斑林狸"
			],
			"quiz": " 在我国已灭绝的10种野生动物中，新疆占了3种，它们是野马、高鼻羚羊和（）"
		},

                                {
			"answer": 2,
			"options": [
                                                                "任何单位",
				"单位和个人",
				"个人"
			],
			"quiz": " 野生动物及其生存环境保护，禁止（）非法猎捕或者破坏"
		},

                                {
			"answer":3 ,
			"options": [
                                                                "自己带回家中收养",
				"置之不理",
				"及时报告当地野生动物行政主管部门"
			],
			"quiz": "任何单位和个人发现受伤、病弱、饥饿、受困、迷途的国家和地方重点保护野生动物时，应当（）"
		},

                                {
			"answer": 3,
			"options": [
                                                                "6",
				"7",
				"8",
				"9"
			],
			"quiz": "最早的虎栖息于亚洲东北部，后来逐渐分为两支，分别向西、向南方扩展，形成目前的（）个亚种"
		},

                                {
			"answer":4 ,
			"options": [
                                                                "5-10",
				"10-15",
				"15-20",
				"20-30"
			],
			"quiz": "估计世界上仅有（）只野生华南虎，现在可能连这个数量也不到了"
		},

                                {
			"answer": 1,
			"options": [
                                                                "是",
				"不是"
			],
			"quiz": "江豚是哺乳动物吗？"
		},

                                {
			"answer": 1,
			"options": [
                                                                "中国",
				"美国",
				"印度",
				"加拿大"
			],
			"quiz": "虎的故乡在（）"
		},

                                {
			"answer": 4,
			"options": [
                                                                "东北虎",
				"华南虎",
				"新疆虎",
				"孟加拉虎"
			],
			"quiz": "我们所知所有的白虎都是（）的变种，仅是颜色上的改变"
		},
]
	var questionSum = allQuestion.length; //题目总数
	
	document.getElementById("gamePage").style.display = "none";
	
	$('.startBtn').click(() => {
		document.getElementById("gamePage").style.display = "block";
		$('.startPage').fadeOut(500)
		alert("共" + questionCount + "题,每题" + everyScore + "分");
		getCurrentQuestion()
		insertItem(currentQuestion)
		timer();
	})

	var timer = () => {
		setInterval(() => {
			$('.time').html(
				`<div class="remainder-time">剩余时间: ${countTime}</div>
											 <div class="remainder-question">剩余题目: ${questionCount-count}</div>`
			)
			countTime--
			$('.answer').on('click', (e) => {
				if (e.target.className == "item") {
					countTime = 10
				}
			})
			if (countTime == -1) {
				countTime = 10;
                var que = currentQuestion[count];
				alert("超时啦！正确答案为:" + que.options[que.answer - 1]);
				count++;
				if (count < questionCount)
					nextQuestion()
				else {
					getScore()
				}
			}
		}, 1000)
	}

	//获取题目
	var getCurrentQuestion = () => {
		var newQuestion = [];
		var arr = new Array(questionCount);
		for (var a = 0; a < arr.length; a++) {
			arr[i] = 0;
		}
		for (var i = 0; i < questionCount; i++) {
			var randomNum = parseInt((Math.random() * 10) % questionSum);
			while (arr[randomNum] == 1) {
				randomNum = parseInt((Math.random() * 10) % questionSum);
			}
			newQuestion.push(allQuestion[randomNum]);
			arr[randomNum] = 1;
			currentQuestion = newQuestion;
		}
	}


	//渲染题目和答案
	var insertItem = (data) => {
		$('.question').html(`
				${data[count].quiz}
		`)
		var dataOptions = data[count].options;
		for (var i = 0; i < dataOptions.length; i++) {
			$('.answer').append(`
					<button class="item" data-index="${i+1}">${dataOptions[i]}</button>
			`)
		}
	}

	//显示点击的答案
	var showAnswer = (e) => {
		var que = currentQuestion[count];
		if (e.target.dataset.index == que.answer) {
			$(e.target).delay("1500").css({
				backgroundColor: 'springgreen'
			})
			score += everyScore;
		} else {
			$(e.target).delay("1500").css({
				backgroundColor: 'red'
			});
			
			alert("答错啦! 正确答案为:" + que.options[que.answer - 1]);

		}
	}


	//显示分数
	var getScore = () => {
		alert("你的分数是" + score);

		$('.gamePage').fadeOut(300);
		window.location.reload();
	}


	//获得下一问题
	var nextQuestion = () => {
		$('.question').html("");
		$('.answer').html("");
		insertItem(currentQuestion);
		isConfirm = false;
	}

	$('.answer').click((e) => {
		if (e.target.className == "item") {
			if (!isConfirm) {
				showAnswer(e)
				count++;
				if (count < questionCount) {
					setTimeout(() => {
						nextQuestion()
					})
				} else if (count == questionCount) {
					setTimeout(() => {
						getScore();
					})
				}
			}
			isConfirm = true;
		}
	})


})
